package com.xtkj.superclub_friendscircle.service.Impl;

import com.xtkj.superclub_entity.entity.friendsCircle.LikedStatusEnum;
import com.xtkj.commons.RedisKeyUtils;
import com.xtkj.superclub_entity.entity.friendsCircle.DTO.LikedCountDTO;
import com.xtkj.superclub_entity.entity.friendsCircle.UserLike;
import com.xtkj.superclub_friendscircle.service.ILike2DBService;
import com.xtkj.superclub_friendscircle.service.ILikes2RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目名称：superclub
 * 类 名 称：RedisServiceImpl
 * 类 描 述：TODO
 * 创建时间：2020/1/3 13:44
 * 创 建 人：xiezhiyan
 */
@Service
@Slf4j
public class Likes2RedisServiceImpl implements ILikes2RedisService {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ILike2DBService likedService;

    @Override
    public void saveLiked2Redis(String likedUserId, String shareId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, shareId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(String likedUserId, String shareId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, shareId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.UNLIKE.getCode());
    }

    @Override
    public void deleteLikedFromRedis(String likedUserId, String shareId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, shareId);
        redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
    }

    @Override
    public void incrementLikedCount(String shareId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, shareId, 1);
    }

    @Override
    public void decrementLikedCount(String shareId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, shareId, -1);
    }

    @Override
    public List<UserLike> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<UserLike> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> entry = cursor.next();
            String key = (String) entry.getKey();
            //分离出 likedUserId，shareId
            String[] split = key.split("::");
            String shareId = split[0];
            String likedUserId = split[1];
            Integer value = (Integer) entry.getValue();

            //组装成 UserLike 对象
            UserLike userLike = new UserLike(shareId,likedUserId,value,new Date(),new Date());
            list.add(userLike);

            //存到 list 后从 Redis 中删除
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
        }
        return list;
    }

    @Override
    public List<LikedCountDTO> getLikedCountFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, ScanOptions.NONE);
        List<LikedCountDTO> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> map = cursor.next();
            //将点赞数量存储在 LikedCountDTO
            String key = (String)map.getKey();
            LikedCountDTO dto = new LikedCountDTO(key, (Integer) map.getValue());
            list.add(dto);
            //从Redis中删除这条记录
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
        }
        return list;
    }
}